package com.hotel.front;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.DNRLogic;
import com.hotel.BL.GenericLogic;
import com.hotel.BL.ReservationLogic;

import data.DNR;
import data.SubReservation;

@SuppressWarnings("serial")
public class DNRService extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String func = req.getParameter("func");
			if (func.equals("AddDNR")) {
				AddDNR(req, resp);
			}
			else if(func.equals("GetRoomDNR")){
				GetRoomDNR(req,resp);
			}
			else if(func.equals("GetExistDNR")){
				GetExistDNR(req,resp);
			}
			else if(func.equals("DelDNR")){
				DelDNR(req,resp);
			}
			else if(func.equals("UpdateDNR")){
				UpdateDNR(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void AddDNR(HttpServletRequest req, HttpServletResponse resp)
			throws IOException, JSONException, ServletException,
			EntityNotFoundException, ParseException {
		HttpSession session = req.getSession(true);
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		long roomId = Long.parseLong(req.getParameter("roomId"));
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH);
		Date datefrom = formatter.parse(req.getParameter("datefrom"));
		Date dateto = formatter.parse(req.getParameter("dateto"));
		String description = req.getParameter("desc");
		DNRLogic dnrLogic = new DNRLogic(req,resp);
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		
		List<SubReservation> list = resLogic.GetReservationWithinDateInSpecificRoom(hotelId,datefrom,Util.daysBetween(datefrom, dateto),roomId);
		Object obj = null;
		if(list.size()==0)
			obj = dnrLogic.AddDNR(hotelId, roomId, datefrom, dateto, description);
		
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetRoomDNR(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session = req.getSession(true);
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		long roomId = Long.parseLong(req.getParameter("roomid"));
		String dnrId = req.getParameter("dnr");
		DNRLogic dnrLogic = new DNRLogic(req,resp);
		GenericLogic gLogic = new GenericLogic(req,resp);
		Date currentdate = Util.GetOnlyDate(gLogic.ServerDateFromNightAudit(hotelId));
		
		List<DNR> list = dnrLogic.GetExistDNR(hotelId, currentdate, roomId);
		HashMap<String,Object> hash = new HashMap<String,Object>();
		hash.put("List", list);
		if(dnrId!=null)
			hash.put("DNR", dnrLogic.GetDNR(Long.parseLong(dnrId)));
		
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(hash));
	}
	
	private void GetExistDNR(HttpServletRequest req, HttpServletResponse resp) throws IOException{
		HttpSession session = req.getSession(true);
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		DNRLogic dnrLogic = new DNRLogic(req,resp);
		GenericLogic gLogic = new GenericLogic(req,resp);
		Date currentdate = Util.GetOnlyDate(gLogic.ServerDateFromNightAudit(hotelId));
		
		List<DNR> list = dnrLogic.GetExistDNR(hotelId, currentdate);
				
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(list));
	}

	private void DelDNR(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		long dnrId = Long.parseLong(req.getParameter("dnr"));
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		try {
			resp.getWriter().println(gson.toJson(new DNRLogic(req,resp).DeleteRoom(dnrId)));
		} catch (Exception e) {
			resp.getWriter().println(gson.toJson(false));
		}
	}
	
	private void UpdateDNR(HttpServletRequest req, HttpServletResponse resp) throws ParseException, IOException{
		HttpSession session = req.getSession(true);
		long hotelId = 1;
		if (session.getAttribute("hotelId") != null)
			hotelId = (Long) session.getAttribute("hotelId");
		long dnrId = Long.parseLong(req.getParameter("dnrId"));
		long roomId = Long.parseLong(req.getParameter("roomId"));
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd",
				Locale.ENGLISH);
		Date datefrom = formatter.parse(req.getParameter("datefrom"));
		Date dateto = formatter.parse(req.getParameter("dateto"));
		String description = req.getParameter("desc");
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		DNRLogic dnrLogic = new DNRLogic(req,resp);

		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		DNR rs = null;
		List<SubReservation> list = resLogic.GetReservationWithinDateInSpecificRoom(hotelId,datefrom,Util.daysBetween(datefrom, dateto),roomId);
		if(list.size() == 0)
		{
			DNR dnr = dnrLogic.GetDNR(dnrId);
			dnr.setDate(datefrom);
			dnr.setDateTo(dateto);
			dnr.setDescription(description);
			rs = dnrLogic.UpdateDNR(dnr);
			resp.getWriter().println(gson.toJson(rs));
		}
		else if(list.size() == 1)
		{
			if(list.get(0).getParentId() == dnrId)
			{
				DNR dnr = dnrLogic.GetDNR(dnrId);
				dnr.setDate(datefrom);
				dnr.setDateTo(dateto);
				dnr.setDescription(description);
				rs = dnrLogic.UpdateDNR(dnr);
			}
			resp.getWriter().println(gson.toJson(rs));
		}
		else
		{
			resp.getWriter().println(gson.toJson(rs));
		}
	}
}